﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConditionJudge.Question;

namespace ConditionJudge.Answer
{
    public class Albin : IConditionJudge
    {
        public string Judgement(string expression, int n)
        {
            int x = n;
            Stack<string> StCha = new Stack<string>();
            Stack<bool> StBool = new Stack<bool>();
            int j;
            string ex = "";
            bool result = false;
            bool get = false;
            int mark = 0;
            for (int i = 0; i < expression.Length; i++)
            {
                if (expression[i] == ' ')
                    continue;
                else
                    ex += expression[i];
            }
            for (int i = 0; i < ex.Length; i++)
            {
                if (ex[i] == '(')
                {
                    StCha.Push(ex[i].ToString());
                    mark = 0;
                }
                else if (ex[i] == 'X' || ex[i] == 'x')
                {
                    continue;
                }
                else if (ex[i] == '>')
                {
                    j = i;
                    if (ex[j + 1] == '=')
                    {
                        i = i + 2;
                        result = n >= Int32.Parse(ex[i].ToString());
                    }
                    else
                    {
                        i = i + 1;
                        result = n > Int32.Parse(ex[i].ToString());
                    }
                    if (mark != 0)
                    {
                        StCha.Pop();
                        if (mark == 1)
                        {
                            get = StBool.Pop();
                            result = result && get;
                            StBool.Push(result);
                        }
                        else
                        {
                            get = StBool.Pop();
                            result = result || get;
                            StBool.Push(result);
                        }

                    }
                    else
                    {
                        StBool.Push(result);
                    }

                }
                else if (ex[i] == '<')
                {
                    j = i;
                    if (ex[j + 1] == '=')
                    {
                        i = i + 2;
                        result = n <= Int32.Parse(ex[i].ToString());
                    }
                    else
                    {
                        i = i + 1;
                        result = n < Int32.Parse(ex[i].ToString());
                    }
                    if (mark != 0)
                    {
                        StCha.Pop();
                        if (mark == 1)
                        {
                            get = StBool.Pop();
                            result = result && get;
                            StBool.Push(result);
                        }
                        else
                        {
                            get = StBool.Pop();
                            result = result || get;
                            StBool.Push(result);
                        }

                    }
                    else
                    {
                        StBool.Push(result);
                    }
                }
                else if (ex[i] == '!')
                {
                    j = i;
                    i = i + 2;
                    result = n != Int32.Parse(ex[i].ToString());

                    if (mark != 0)
                    {
                        StCha.Pop();
                        if (mark == 1)
                        {
                            get = StBool.Pop();
                            result = result && get;
                            StBool.Push(result);
                        }
                        else
                        {
                            get = StBool.Pop();
                            result = result || get;
                            StBool.Push(result);
                        }

                    }
                    else
                    {
                        StBool.Push(result);
                    }
                }
                else if (ex[i] == '&')
                {
                    i = i + 1;
                    StCha.Push("&&");
                    if (mark == -1)
                    {
                        return "FF";
                    }
                    else
                    {
                        mark = 1;
                    }
                }
                else if (ex[i] == '|')
                {
                    i = i + 1;
                    StCha.Push("||");
                    if (mark == 1)
                    {
                        return "FF";
                    }
                    else
                    {
                        mark = -1;
                    }
                }
                else if (ex[i] == ')')
                {
                    if (StCha.Count != 0)
                    {
                        if (StCha.Peek() != "(")
                        {
                            if (StCha.Peek() == "&&")
                            {
                                mark = 1;
                            }
                            else
                            {
                                mark = -1;
                            }
                        }
                        else
                        {

                            StCha.Pop();
                            if (StCha.Count != 0)
                            {
                                if (StCha.Pop() == "&&")
                                {
                                    get = StBool.Pop();
                                    result = StBool.Pop();
                                    result = get && result;
                                    mark = 1;
                                }
                                else if (StCha.Pop() == "||")
                                {
                                    get = StBool.Pop();
                                    result = StBool.Pop();
                                    result = get || result;
                                    mark = -1;
                                }
                                else
                                {
                                    mark = 0;
                                }
                            }
                        }
                    }
                }

            }
            if (result == true)
            {
                return "TT";
            }


            else
            {
                return "TF";
            }


        }
    }
}
